﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VersionComparer.DataModel;
using VersionComparer.UI.ExtendedTreeView;

namespace VersionComparer
{
    public partial class SearchControl : UserControl
    {
        #region fields
        //ugly hack to allow focusing a control on load
        Timer focustimer = new Timer();
        #endregion fields

        #region constructors
        public SearchControl()
        {
            InitializeComponent();
        }
        #endregion constructors

        #region properties
        /// <summary>
        /// Default text to search
        /// </summary>
        public string DefaultText
        {
            get;
            set;
        }
        /// <summary>
        /// Main treeview to search
        /// </summary>
        public MultiSelectTreeView TreeView
        {
            get;
            set;
        }
        /// <summary>
        /// Node to start the search
        /// </summary>
        public TreeNode StartingNode
        {
            get
            {
                if (TreeView.Nodes.Count == 0)
                    return null;
                return (TreeView.SelectedNode ?? TreeView.TopNode);
            }
        }
        /// <summary>
        /// Current search text
        /// </summary>
        public string CurrentText
        {
            get;
            set;
        }
        #endregion properties

        #region event handlers
        private void btNext_Click(object sender, EventArgs e)
        {
            MoveNext(null);
        }
        private void btPrevious_Click(object sender, EventArgs e)
        {
            MovePrevious(null);
        }

        /// <summary>
        /// Hack required to allow focusing the first control on load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void focustimer_Tick(object sender, EventArgs e)
        {
            txtSearch.Text = DefaultText;
            txtSearch.SelectAll();
            txtSearch.Focus();
            focustimer.Stop();
        }
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            CurrentText = txtSearch.Text;
        }
        protected override void OnLoad(EventArgs e)
        {
            
            focustimer.Interval = 100;
            focustimer.Tick += new EventHandler(focustimer_Tick);
            focustimer.Start();
            base.OnLoad(e);
        }

        #endregion event handlers

        /// <summary>
        /// Move to the next item matching text
        /// </summary>
        /// <param name="text">text to match</param>
        public void MoveNext(string text)
        {
            #region validations
            if (String.IsNullOrEmpty(text))
                text = txtSearch.Text;
            if (String.IsNullOrEmpty(text))
                return;
            #endregion validations

            TreeNode startingNode = StartingNode;
            if (startingNode == null)
                return;
            string[] values = text.Split('*');
            TreeNode currentNode = startingNode;
            #region main search cycle
            do
            {
                currentNode = TreeView.GetNextNode(currentNode, true);
                ComparableItem xmlNode = (currentNode.Tag as ComparableItem);
                if (xmlNode.ContainsString(cbIgnoreCase.Checked, values) ||
                    xmlNode.RelatedItem.ContainsString(cbIgnoreCase.Checked, values))
                {
                    TreeView.SelectedNode = currentNode;
                    currentNode.Expand();
                    currentNode.EnsureVisible();
                    return;
                }
            }
            while (currentNode != startingNode);
            #endregion main search cycle

            MessageBox.Show(Resources.Resources.NoMatchesFound, "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        /// <summary>
        /// Move to the previous item matching text
        /// </summary>
        /// <param name="text">text to match</param>
        public void MovePrevious(string text)
        {
            #region validations
            if (String.IsNullOrEmpty(text))
                text = txtSearch.Text;
            if (String.IsNullOrEmpty(text))
                return;
            #endregion validations
            TreeNode startingNode = StartingNode;
            if (startingNode == null)
                return;
            string[] values = text.Split('*');
            TreeNode currentNode = startingNode;
            do
            {
                currentNode = TreeView.GetPreviousNode(currentNode);
                ComparableItem xmlNode = (currentNode.Tag as ComparableItem);
                if (xmlNode.ContainsString(cbIgnoreCase.Checked, values) || xmlNode.RelatedItem.ContainsString(cbIgnoreCase.Checked, values))
                {
                    TreeView.SelectNode(currentNode);
                    currentNode.Expand();
                    currentNode.EnsureVisible();
                    return;
                }
            }
            while (currentNode != startingNode);
            MessageBox.Show(Resources.Resources.NoMatchesFound, "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }
}
